package com.msf.tutorial.java8.chapter2;

import com.msf.tutorial.java8.Apple;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class ComparatorExample {

    public static void main(String[] args) {

        List<Apple> list = Arrays.asList(
                new Apple(1L, "red", 1D),
                new Apple(2L, "red", 2D),
                new Apple(3L, "green", 3D),
                new Apple(1L, "green", 3D)
        );

        // 原来的方式
        list.sort(new Comparator<Apple>() {
            public int compare(Apple a1, Apple a2){
                return a1.getWeight().compareTo(a2.getWeight());
            }
        });

        // 使用 Lambda 表达式
        list.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));

        // 也可以这么写
        list.sort((a1, a2) -> a1.getWeight().compareTo(a2.getWeight()));

        // 还可以这么写
        list.sort(Comparator.comparing(Apple::getWeight));

        System.out.println(list);

    }

}
